<?php
if (!defined("LEVEL") || !defined("OK")) { header("Location: ?"); }
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = escape(ceil((int)$url['p'])); } else { $p = 0; }
$limit = 50;
$viso = kiek("chat_box");

//jei tai moderatorius
if (LEVEL > 20) {
	//jei paspaude trinti
	if (isset($url['d']) && !empty($url['d']) && isnum($url['d'])) {
		$id = (int)$url['d'];
		mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."chat_box` WHERE `id` = ".escape($id)." LIMIT 1");
		if (mysql_affected_rows() > 0) { msg("Administravimas","Žinutė: $id ištrinta","informacija"); } else { klaida("Klaida","Žinutė nebuvo pakeista"); }
		redirect("?id,49;p,$p",$_SERVER['HTTP_REFERER']);
	}
	//Jei adminas paspaude redaguoti
	if (isset($url['r']) && !empty($url['r']) && $url['r'] > 0 && isnum($url['r'])) {
		$nick = $_SESSION['username']; $nick_id = $_SESSION['id'];
		if(empty($_POST)) {
			$msg = mysql_fetch_assoc(mysql_query1("SELECT `msg` FROM `".LENTELES_PRIESAGA."chat_box` WHERE `id`=".escape(ceil((int)$url['r']))." LIMIT 1"));
			$msg = '<form name="chat_box_edit" action="" method="post">
					<textarea name="msg" rows="3" cols="25" wrap="on" style="width:265px">'.input($msg['msg']).'</textarea>
					<br />
					<input type="submit" name="chat_box" value="Redaguoti" />
					</form>
					';

			$text = $msg;
			lentele("Žinutės redagavimas",$text);
		}
		elseif (isset($_POST['chat_box']) && $_POST['chat_box'] == 'Redaguoti' && !empty($_POST['msg'])) {
			$msg = trim($_POST['msg'])."\n[sm][i]Redagavo: ".$_SESSION['username']."[/i][/sm]";
			mysql_query1("UPDATE `".LENTELES_PRIESAGA."chat_box` SET `msg` = ".escape($msg)." WHERE `id` =".escape($url['r'])." LIMIT 1");
			if (mysql_affected_rows() > 0) { msg("Informacija","Žinutė sėkmingai pakeista"); } else { klaida("Klaida","Žinutė nebuvo pakeista"); }
			//redirect("?id,49;p,$p#".escape($url['r'])."","meta");
		}
	}
}
//Atvaizduojam pranesimus su puslapiavimu - LIMITAS nurodytas virsuje
$sql2 = mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."chat_box` ORDER BY `time` DESC LIMIT $p, $limit");
if ($viso > $limit) { lentele("Puslapiai",puslapiai($p,$limit,$viso,10)); }

$text = '';
while ($row = mysql_fetch_assoc($sql2)) {
	$extra = '';
	if ($row['nikas'] != $_SESSION['username']) { $extra = "<a href=\"?id,45;n,1;u,".$row['nikas']."\"><img src=\"images/pm/mail.png\" alt=\"pm\" border=\"0\" /></a> "; }
	if (LEVEL == 30) { $extra .= "<a href='".url("d,".$row['id']."")."'><img src='images/icons/bullet_delete.png' alt='[d]' title='trinti' class='middle' border='0' /></a> <a href='".url("r,".$row['id']."")."'><img src='images/icons/bullet_orange.png' alt='[r]' title='redaguoti' class='middle' border='0' /></a> <a href='?id,49;b,".$row['niko_id']."'><img src='images/icons/bullet_red.png' alt='[b]' title='baninti' class='middle' border='0' /></a> "; }
	else { $extra = ''; }
	$text .= "<div class=\"title\"><em><a href=\"?id,49;p,$p#".$row['id']."\" name=\"".$row['id']."\" id=\"".$row['id']."\"><img src=\"images/icons/bullet_black.png\" alt=\"#\" class=\"middle\" border=\"0\" /></a> <a href=\"?id,47;m,".$row['niko_id']."\" title=\"Apie ".$row['nikas']."\">".$row['nikas']."</a> $extra (".$row['time'].") - ".kada($row['time'])."</em></div><div class=\"sarasas\">".smile(bbchat($row['msg']))."</div><br/>";

}
lentele("Šaukyklos dėžutės archyvas",$text);
if ($viso > $limit) { lentele("Puslapiai",puslapiai($p,$limit,$viso,10)); }
unset($extra,$text);
//PABAIGA - atvaizdavimo

?>